package cn.dankal.basiclib.base.recyclerview;

import android.content.Context;

import androidx.annotation.IdRes;
import androidx.core.graphics.ColorUtils;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.ButterKnife;

public abstract class BaseRecyclerViewHolder<T> extends RecyclerView.ViewHolder {

    private int viewType;
    private boolean isRecycled;

    public BaseRecyclerViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    public BaseRecyclerViewHolder(Context context, ViewGroup viewGroup, int layoutResId) {
        super(LayoutInflater
                .from(context)
                .inflate(layoutResId, viewGroup, false));
    }

    public BaseRecyclerViewHolder(Context context, int layoutResId) {
        this(context, null, layoutResId);
    }

    public BaseRecyclerViewHolder(View itemView, int viewType) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.viewType = viewType;
    }

    public final <V extends View> V findViewById(int resid) {
        if (resid > 0 && itemView != null) {
            return itemView.findViewById(resid);
        }
        return null;
    }

    public abstract void onBindData(T data, int position);

    protected int getViewType() {
        return viewType;
    }

    protected Context getContext() {
        return itemView.getContext();
    }

    public boolean isRecycled() {
        return isRecycled;
    }

    public void setRecycled(boolean recycled) {
        isRecycled = recycled;
    }

    public void setText(@IdRes int view, String text) {
        TextView textView = itemView.findViewById(view);
        textView.setText(text);
    }

    public void setTextByHtml(@IdRes int view, String text) {
        TextView textView = itemView.findViewById(view);
        if (TextUtils.isEmpty(text))
            return;
        textView.setText(Html.fromHtml(text));
    }

    public void setTextDeleteStyle(@IdRes int view) {
        TextView textView = itemView.findViewById(view);
        textView.getPaint().setAntiAlias(true);
        textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
    }

    public void setTextViewColor(@IdRes int view, int textColor) {
        TextView textView = itemView.findViewById(view);
        textView.setTextColor(textView.getContext().getResources().getColor(textColor));
    }

    public void setVisibility(@IdRes int view, boolean isVisible) {
        View view1 = itemView.findViewById(view);
        view1.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    public void setGone(@IdRes int view, boolean isGone) {
        View view1 = itemView.findViewById(view);
        view1.setVisibility(isGone ? View.GONE : View.VISIBLE);
    }

    public void setInvisible(@IdRes int view, boolean isInvisible) {
        View view1 = itemView.findViewById(view);
        view1.setVisibility(isInvisible ? View.INVISIBLE : View.GONE);
    }

    public void setImageUrl(@IdRes int view, String url) {
        ImageView view1 = itemView.findViewById(view);
        if (TextUtils.isEmpty(url)) return;
        Glide.with(getContext()).load(url).into(view1);
    }

    public void setImageBitmapRes(@IdRes int view, Bitmap url) {
        ImageView view1 = itemView.findViewById(view);
        view1.setImageBitmap(url);
    }

    public void setImageResId(@IdRes int view, int url) {
        ImageView view1 = itemView.findViewById(view);
        view1.setImageResource(url);
    }
}
